class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int left=0;
        int right=0;
        int ret=-1;
        int n=nums.size();
        int sum=0;
        int mysum=0;
        for(int i=0;i<n;++i)
        {
            sum+=nums[i];
        }
        int target=sum-x;
        if(target<0)
        {
            return -1;
        }
        if(sum==x)
        {
            return n;
        }
        while(right<n)
        {
            mysum+=nums[right];
            while(mysum>target)
            {
                mysum-=nums[left++];
            }
            if(mysum==target)
            {
                ret=max(ret,(right-left+1));
            }
            right++;
        }
        if(ret==-1)
            return ret;
        else
            return nums.size()-ret;
    }
};
